

# 基本语法
try:
    f = open("D:/ajkjkladlkfsl.txt", "r", encoding='UTF-8')
except:
    print(f"出现异常了，因为文件不存在，我将open的模式，改为w模式去打开")
    f = open("D:/ajkjkladlkfsl.txt", "w", encoding='UTF-8')

# 捕获指定的异常
try:
    print(name)
except NameError as e:
    print("出现了变量未定义的异常")

try:
    1 / 0
except ZeroDivisionError as a:
    print("出现了0除异常")


# 捕获多个异常
try:
    print(hh)
    20 / 0
except (NameError, ZeroDivisionError) as j:
    print("出现了变量未定义的异常 或 0除异常")

# 捕获所有异常
try:
    1 / 0
    print(name)
    open
except Exception as e:  # Exception 是顶级异常
    print("出现异常了")
else:
    print("好高兴，没有出现异常")
finally:
    f.close()

